
package com.bryanprogramming.zend.gui;

import com.bryanprogramming.zend.logic.Player;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.DecimalFormat;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author charlesbryan
 */
public class CoinPurchaseFrame extends JPanel implements PropertyChangeListener {

    private JLabel coinsLabel;
    private Player player;


    private CoinPurchaseFrame(Player p) {
        p.addPropertyChangeListener(this);
        this.player = p;
        
        buildPanel();
    }

    private void buildPanel() {
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(300, 200));

        JPanel buttonPanel = new JPanel(new GridLayout(3, 1, 20, 10));

        JButton button = new JButton("Purchase 2,000 Coins! ($0.99)");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                player.addCoins(2_000);
            }
        });
        buttonPanel.add(button);

        button = new JButton("Purchase 15,000 Coins! ($4.99)");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                player.addCoins(15_000);
            }
        });        
        buttonPanel.add(button);

        button = new JButton("Purchase 40,000 Coins! ($9.99)");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                player.addCoins(40_000);
            }
        });        
        buttonPanel.add(button);
        
        this.add(buttonPanel, BorderLayout.CENTER);

         this.add(buildStatusPanel(), BorderLayout.SOUTH);
    }
    
    private JPanel buildStatusPanel() {
        JPanel statusPanel = new JPanel(new BorderLayout());
        statusPanel.setBackground(Color.LIGHT_GRAY);
        statusPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.DARK_GRAY));
        
        Font font = new Font("San-Serif", Font.BOLD, 10);
        FontMetrics fm = this.getFontMetrics(font);
        int width = fm.stringWidth(" SCORE: 999,999");
        int height = fm.getHeight();
       
        
        JPanel coinsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        coinsPanel.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.GRAY));
        coinsPanel.setBackground(Color.LIGHT_GRAY);
        
        width = fm.stringWidth("Gold Coins: 1,999,999");
        height = fm.getHeight();
        
        coinsLabel = new JLabel("Gold Coins: " + Game.FMT.format(this.player.getGoldCoins()));
        coinsLabel.setFont(font);
        coinsLabel.setBackground(Color.LIGHT_GRAY);
        coinsLabel.setPreferredSize(new Dimension(width, height));
        coinsPanel.add(coinsLabel);
        statusPanel.add(coinsPanel, BorderLayout.WEST);
        
        return statusPanel;        
    } 

    public static void createAndShowGUI(Player p) {
        //Create and set up the window.

        JFrame frame = new JFrame("Z-End");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new CoinPurchaseFrame(p);
        newContentPane.setOpaque(true); //content panes must be opaque

        //frame.setJMenuBar(((Game) newContentPane).buildMenu());
        frame.setContentPane(newContentPane);
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(Player.Properties.COINS_AMOUNT_CHANGED)) {
            coinsLabel.setText("Gold Coins: " + Game.FMT.format(this.player.getGoldCoins()));
        }
    }
}
